package de.h_da.fbi.cg.pointcloud.adt;

/**
 * @author Nicolas Gramlich
 * @since 09:54:21 - 11.11.2010
 */
public class Edge implements Comparable<Edge> {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	public final Vertex mVertexA;
	public final Vertex mVertexB;
	public final float mLength;
	public boolean mEuclideanMinumumSpanningTreeEdge = false;

	private final int mHashCode;

	// ===========================================================
	// Constructors
	// ===========================================================

	public Edge(final Vertex pVertexA, final Vertex pVertexB) {
		this.mVertexA = pVertexA;
		this.mVertexB = pVertexB;

		final float dX = pVertexA.mX - pVertexB.mX;
		final float dY = pVertexA.mY - pVertexB.mY;
		final float dZ = pVertexA.mZ - pVertexB.mZ;

		this.mLength = (float)Math.sqrt(dX * dX + dY * dY + dZ * dZ);

		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(this.mLength);
		result = prime * result + ((this.mVertexA == null) ? 0 : this.mVertexA.hashCode());
		result = prime * result + ((this.mVertexB == null) ? 0 : this.mVertexB.hashCode());
		this.mHashCode = result;
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public int compareTo(final Edge pOtherEdge) {
		final float dLength = this.mLength - pOtherEdge.mLength;
		if(dLength < 0) {
			return -1;
		} else if(dLength > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public int hashCode() {
		return this.mHashCode;
	}

	@Override
	public String toString() {
		return "[" + this.mVertexA.toString() + "-->" + this.mVertexB.toString() + "]";
	}

	// ===========================================================
	// Methods
	// ===========================================================

	public boolean isIntraComponentEdge() {
		return this.mVertexA.mGraph == this.mVertexB.mGraph;
	}

	public boolean isInterComponentEdge() {
		return this.mVertexA.mGraph != this.mVertexB.mGraph;
	}

	public boolean contains(final Vertex pVertex) {
		return pVertex == this.mVertexA || pVertex == this.mVertexB;
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
